﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;
using System.Security;

using PasswordProvider.ObjectModel;
using PasswordProvider.ViewModel.Properties;
using PasswordProvider.ViewModel.Framework;

namespace PasswordProvider.ViewModel
{
    public class EnterPasswordViewModel : VisualViewModel, IRequestClose
    {
        #region Private Fields, Construction

        private string _openFilePath;
        private Predicate<ProtectedString> _onPasswordEnteredCallback;

        internal EnterPasswordViewModel(bool showAttentionGetter)
            : this(showAttentionGetter, null)
        {
        }

        internal EnterPasswordViewModel(bool showAttentionGetter, Predicate<ProtectedString> onPasswordEnteredCallback)
        {
            ShowAttentionGetter = showAttentionGetter;
            _onPasswordEnteredCallback = onPasswordEnteredCallback;
        }
        internal EnterPasswordViewModel(string openFilePath)
        {
            _openFilePath = openFilePath;
        }
       
        #endregion // fields, construction

        #region ICompleteTask Members

        public event EventHandler CloseRequested;

        #endregion

        #region Public Properties

        /// <summary>
        /// The password
        /// </summary>
        public PasswordEntryViewModel Password
        {
            get
            {
                if (_password == null)
                {
                    _password = new PasswordEntryViewModel(Strings.EnterPassword_Password_DisplayName);
                }
                return _password;
            }
        } private PasswordEntryViewModel _password;

        /// <summary>
        /// The wrong password warning
        /// </summary>
        public VisualViewModel Warning
        {
            get
            {
                if (_warning == null)
                {
                    _warning = new VisualViewModel(
                        Strings.EnterPassword_WrongPasswordWarning,
                        Visibility.Collapsed);
                }
                return _warning;
            }            
        } private VisualViewModel _warning;

        public bool ShowAttentionGetter
        {
            get;
            set;
        }

        /// <summary>
        /// The OK Command
        /// </summary>
        public CommandViewModel OkCommand
        {
            get
            {
                if (_okCommand == null)
                {
                    _okCommand = new CommandViewModel(
                        Strings.General_Ok,
                        new RelayCommand(param=>OkCommandExecuted(), param=>OkCommandCanExecute()));
                }
                return _okCommand;
            }
        } private CommandViewModel _okCommand;

       
        #endregion // Public properties

        #region Private Methods

        private void OkCommandExecuted()
        {
            ProtectedString protectedPassword = new ProtectedString(Password.Password);
            bool result;

            if (_onPasswordEnteredCallback != null)
                result = _onPasswordEnteredCallback(protectedPassword);
            else if (_openFilePath != null)
                result = PasswordProviderData.Open(protectedPassword, _openFilePath);
            else
                result = PasswordProviderData.Current.SecurityManager.Authenticate(protectedPassword);
            
            if(result)
            {
                // Raise the task complete event.
                if(CloseRequested != null)
                    CloseRequested(this, new EventArgs());
            }
            else
            {
                // Show the warning.
                Warning.Visibility = Visibility.Visible;
            }
        }

        private bool OkCommandCanExecute()
        {
            return ((Password.Password != null) && Password.Password.Length > 0);
        }


        #endregion
    }
}
